package cn.wolfcode.crm.service.imp;


import cn.wolfcode.crm.domain.Dictionary;
import cn.wolfcode.crm.domain.DictionaryBill;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.DictionaryBillMapper;
import cn.wolfcode.crm.mapper.DictionaryMapper;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictionaryServiceImpl implements IDictionaryService {
    @Autowired
    private DictionaryMapper dictionaryMapper;
    @Autowired
    private DictionaryBillMapper dictionaryBillMapper;
    @Override

    public void deleteByPrimaryKey(Long id) {
        if (id != null) {
            Dictionary dictionary = dictionaryMapper.selectByPrimaryKey(id);

            //维护与明细之间的关系  通过sn删除
            dictionaryBillMapper.deleteBySn(dictionary.getSn());
        dictionaryMapper.deleteByPrimaryKey(id);
        }
    }

    @Override

    public void saveOrUpdate(Dictionary entity) {
        if (entity.getId() != null) {
            //更新操作
            dictionaryMapper.updateByPrimaryKey(entity);
            //维护与明细之间的关系 通过sn删除
            dictionaryBillMapper.deleteBySn(entity.getSn());

        }else {

            dictionaryMapper.insert(entity);
        }
        //更新与新增都需要插入
        if (entity.getDictionaryBills() != null) {

            for (DictionaryBill bill : entity.getDictionaryBills()) {
                //插入many方对象
                dictionaryBillMapper.insert(bill);
            }
        }
    }

    @Override
    public Dictionary selectByPrimaryKey(Long id) {

            Dictionary dept = dictionaryMapper.selectByPrimaryKey(id);
        return dept;

    }

    @Override
    public List<Dictionary> selectAll() {
        List<Dictionary> depts = dictionaryMapper.selectAll();
        return depts;
    }




}
